﻿##此DEMO的功能：查询手机类的淘客商品
# -*- coding: utf-8 -*-
import urllib, urllib2, string, time, md5, re, types, logging
from iids import iids
#获得当前时间
t = time.localtime()

#参数数组
paramArray = {
	'app_key':'12022540',
	'method':'taobao.taobaoke.items.convert',
	'format':'json',
	'v':'2.0',
	'timestamp':time.strftime('%Y-%m-%d %X', t),
	
	'fields':'iid,title,nick,pic_url,price,click_url',
	'nick':'xiaojay2006',
  'iids':iids
}

#签名函数
def _sign(param,sercetCode):
	src = sercetCode + ''.join(["%s%s" % (k, v) for k, v in sorted(param.items())])
	return md5.new(src).hexdigest().upper()
	

#生成签名
sign = _sign(paramArray, '6f7a04f813d9a9fc916e0b15854e9f6f')
paramArray['sign'] = sign

#组装参数
form_data = urllib.urlencode(paramArray)

#访问服务
urlopen = urllib2.urlopen('http://gw.api.taobao.com/router/rest', form_data)

rsp_ = urlopen.read()
rsp_ = rsp_.decode('UTF-8')
print rsp_


import simplejson as json

rsp = json.loads(rsp_)

data = rsp['taobaoke_items_convert_response']
total = data['total_results']
items = data['taobaoke_items']['taobaoke_item']
i = items[0]

print total

for i in items:
  print i.title
  print '\t', i.click_url
